<?php

namespace app\controller\admin\basic;

use app\model\admin\basic\BasicLabel;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use laytp\library\Tree;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Category extends Backend
{
    protected $model;
    protected $noNeedAuth = ['index', 'info','type','tree','teamTree'];
    protected $type = 1;
    protected static $use_type_map = [1 => '通用', 2 => '主角色标签专用分类', 3 => '阵容搭配图鉴专用'];

    public function initialize()
    {
        $this->model = new BasicLabel();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public static function getUseType(){
        return self::$use_type_map;
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
//            dd($where);
            $data = $this->model->where($where)->where('type', $this->type)->order($order)->cache(true)->select()->toArray();
            foreach ($data as &$item) {
                $item['name'] = $item['name']."(".self::$use_type_map[$item['use_type']].")";
            }
        } else {
            $limit = $this->request->param('limit', 10);
            $data = $this->model->where($where)->where('type', $this->type)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item) {
                if (isset($item['url']) && !empty($item['url'])) {
                    $item['url'] = imgUrlToAbsolute($item['url']);
                }
                if (isset($item['icon']) && !empty($item['icon'])) {
                    $item['icon'] = imgUrlToAbsolute($item['icon']);
                }
                $item['use_type_name'] = self::$use_type_map[$item['use_type']];
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        if (!$this->request->param("game_id")) {
            return $this->error("缺少游戏id");
        }
        $gameId = $this->request->param("game_id");
        $post = CommonFun::filterPostData($this->request->post());
        try {
            $post['basic_game_id'] = $gameId;
            $post['type'] = $this->type;
            if ($this->model->create($post)) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function type()
    {
        $data = [];
        foreach (self::$use_type_map as $key => $item) {
            $data[] = [
                'name' => $item, 'value' => $key
            ];
        }
        return $this->success('获取分类用途',$data);
    }

    public function teamTree(){
        $post = $this->request->post();
        $where = [
          ['status','=',1]
        ];
        if(isset($post['relation_category'])&&!empty($post['relation_category'])){
            $where[] = [
                'id|pid','in',explode(",",$post['relation_category'])
            ];
        }
        $sourceData = $this->model->where($where)->select()->toArray();
        $menuTreeObj = Tree::instance();
        $menuTreeObj->init($sourceData);
        $data = $menuTreeObj->getRootTrees();
        foreach ($data as $key=>$item){
            if (!isset($item['children'])){
                unset($data[$key]);
            }
        }
        return $this->success('数据获取成功', array_values($data));
    }

    public function tree(){
        $where = $this->buildSearchParams();
        $sourceData = $this->model->where($where)->select()->toArray();
        $childrenData = $this->model->where("status",1)->where("type",2)->whereIn("pid",array_column($sourceData,"id"))->select()->toArray();
        $sourceData =  array_merge($sourceData,$childrenData);
        foreach ($sourceData as $key=>$item){
            if($item['type']==1){
                $sourceData[$key]['name'] = $item['name']."(".self::$use_type_map[$item['use_type']].")";
            }
        }
        $menuTreeObj = Tree::instance();
        $menuTreeObj->init($sourceData);
        $data = $menuTreeObj->getRootTrees();
        foreach ($data as $key=>$item){
            if (!isset($item['children'])){
                unset($data[$key]);
            }
        }
        return $this->success('数据获取成功', array_values($data));
    }
}